//
//  EUExPopTipsView.m
//  EUExPopTipsView
//
//  Created by zhijian du on 15/1/26.
//  Copyright (c) 2015年 zhijian du. All rights reserved.
//

#import "EUExPopTipsView.h"
#import "EUtility.h"
#import "JSON.h"
#import "QBPopupMenu.h"
@implementation EUExPopTipsView
{
    
}
-(id)initWithBrwView:(EBrowserView *)eInBrwView{
    self = [super initWithBrwView:eInBrwView];
    if (self) {
        
    }
    return self;
}

-(void)open:(NSMutableArray *)inArguments{
    NSDictionary *dict = nil;
    NSInteger centerX ,centerY ,textSize;
    NSString *bgColor = nil;
    NSString *textNColor = nil;
    NSString *textHColor = nil;
    NSString *dividerColor = nil;
    NSArray *labelsArray = nil ;
    if (inArguments.count > 0 && [[inArguments objectAtIndex:0] JSONValue] ) {
        dict = [[inArguments objectAtIndex:0] JSONValue] ;
        centerX = [[dict objectForKey:@"centerX"] integerValue];
        centerY = [[dict objectForKey:@"centerY"] integerValue];
        textSize = [[dict objectForKey:@"textSize"] floatValue];
        bgColor = [dict objectForKey:@"bgColor"];
        textNColor = [dict objectForKey:@"textNColor"];
        textHColor = [dict objectForKey:@"textHColor"];
        dividerColor =[dict objectForKey:@"dividerColor"];
        labelsArray = [dict objectForKey:@"labels"];
    }else {//设置默认数据
        centerX = 150;
        centerY = 200;
        textSize = 15;
        bgColor = @"#90000000";
        textNColor = @"#ffffff";
        textHColor = @"#0000C6";
        dividerColor = @"#636363";
        labelsArray = [NSArray arrayWithObjects:@"复制",@"粘贴",@"删除", nil];
    }
    NSLog(@"%@",labelsArray);
    
    QBPopupMenu *pop = [[QBPopupMenu alloc]init];
    pop.bgColor = bgColor;
    pop.dividerColor = dividerColor;
    self.popMenu = pop;
    [pop release];

    NSMutableArray *itemsArray = [[NSMutableArray alloc]init];
    for(int i = 0; i <labelsArray.count ;i++){
        NSString *title = [labelsArray objectAtIndex:i];
        QBPopupMenuItem *item = [QBPopupMenuItem itemWithTitle:title target:self action:@selector(Click:)];
        item.titleNormalColor = [self getColor:textNColor];
        item.tag = i+1;
        item.font = [UIFont systemFontOfSize:textSize];
        [itemsArray addObject:item];
    }
    self.popMenu.items = itemsArray;
    [itemsArray release];
    
    [EUtility brwView:self.meBrwView addSubview:pop];
    
    [self.popMenu showInView:(UIView *)self.meBrwView    atPoint:CGPointMake(centerX, centerY)];
}

-(void)Click:(QBPopupMenuItem *)item{
    NSLog(@"%d",item.tag);
    NSString *tag = [NSString stringWithFormat:@"%d",item.tag];
    [super jsSuccessWithName:@"uexPopTipsView.onItemSelected" opId:0 dataType:0 strData:tag];

}

-(void)close:(NSMutableArray *)inArguments{
    [self.popMenu dismiss];
}

-(void)clean{
    if (self.popMenu) {
        self.popMenu = nil;
    }
}

-(void)dealloc{
    if (self.popMenu) {
        self.popMenu = nil;
    }
    [super dealloc];
}



-(UIImage *)getImageFromLocalFile:(NSString*)imageName{
    return [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];
}
#pragma mark
/**
 *  十六进制字符串转化颜色
 *
 *  @param hexColor 十六进制颜色字符串
 *
 *  @return UIColor
 */
-(UIColor *) getColor:(NSString *)hexColor
{
    unsigned int red, green, blue;
    NSRange range;
    range.length =2;
    range.location =1;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&red];
    range.location =3;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&green];
    range.location =5;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&blue];
    return [UIColor colorWithRed:(float)(red/255.0f)green:(float)(green/255.0f)blue:(float)(blue/255.0f)alpha:1.0f];
    

}
@end
